package cn.wetcland.urban.framework.service.impl;

import cn.wetcland.urban.common.MyAssert;
import cn.wetcland.urban.common.utils.CopyObjectUtil;
import cn.wetcland.urban.framework.dto.DriverAddCarDTO;
import cn.wetcland.urban.framework.mapper.UtCarMapper;
import cn.wetcland.urban.framework.mapper.UtFileUploadMapper;
import cn.wetcland.urban.framework.service.UtCarService;
import cn.wetcland.urban.framework.utils.StpDriverUtil;
import cn.wetcland.urban.framework.vo.CarBrandListItemVO;
import cn.wetcland.urban.framework.vo.CarSerialListItemVO;
import cn.wetcland.urban.framework.vo.DriverCarDetailsVO;
import cn.wetcland.urban.generator.domain.UtCar;
import cn.wetcland.urban.generator.domain.UtFileUpload;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author Wetcland
* @description 针对表【ut_car】的数据库操作Service实现
* @createDate 2024-01-21 09:51:59
*/
@Service
public class UtCarServiceImpl extends ServiceImpl<UtCarMapper, UtCar> implements UtCarService {

    @Resource
    public UtCarMapper carMapper;

    @Resource
    public UtFileUploadMapper fileUploadMapper;

    @Override
    public List<CarBrandListItemVO> carBrandList() {
        return carMapper.selectCarBrandList();
    }

    @Override
    public List<CarSerialListItemVO> carSerialList(Long parentId) {
        return carMapper.selectCarSerialList(parentId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void driverAddCar(DriverAddCarDTO addCarDTO) {
        long loginIdAsLong = StpDriverUtil.getLoginIdAsLong();
        UtCar driverId = carMapper.selectOne(new QueryWrapper<UtCar>()
                .eq("driver_id", loginIdAsLong)
                .eq("is_verify_pass", 1)
        );
        MyAssert.isNull(driverId, "已审核过车辆");
        UtFileUpload utFileUpload = fileUploadMapper.selectById(addCarDTO.getCarPhoto1());
        MyAssert.notNull(utFileUpload, "车前照不存在");
        UtFileUpload utFileUpload1 = fileUploadMapper.selectById(addCarDTO.getCarPhoto2());
        MyAssert.notNull(utFileUpload1, "车后照不存在");
        UtFileUpload utFileUpload2 = fileUploadMapper.selectById(addCarDTO.getCarPhoto3());
        MyAssert.notNull(utFileUpload2, "车左照不存在");
        UtFileUpload utFileUpload3 = fileUploadMapper.selectById(addCarDTO.getCarPhoto4());
        MyAssert.notNull(utFileUpload3, "车右照不存在");
        UtCar utCar = new UtCar();
        CopyObjectUtil.copyPropertiesIgnoreNull(addCarDTO, utCar);
        utCar.setDriverId(loginIdAsLong);
        carMapper.insert(utCar);
    }

    @Override
    public DriverCarDetailsVO driverCarDetails() {
        long loginIdAsLong = StpDriverUtil.getLoginIdAsLong();
        return carMapper.selectDriverCarDetailsByDriverId(loginIdAsLong);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void driverCarDelete() {
        long loginIdAsLong = StpDriverUtil.getLoginIdAsLong();
        carMapper.delete(new QueryWrapper<UtCar>().eq("driver_id", loginIdAsLong));
    }
}




